Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → Arguments by value and reference

Python Functions

Arguments by value and reference

Python Function Arguments: Value vs. Reference Semantics

Understanding how Python handles function arguments is crucial for writing correct and efficient code. Unlike some languages that explicitly declare "pass by value" or "pass by reference," Python employs a mechanism often described as "pass by object reference," which can be nuanced. Let's explore this with examples.

1. Immutable Objects (Pass by Value-like Behavior)

When you pass immutable objects (like integers, floats, strings, tuples) to a function, the function receives a copy of the object's value. Modifying the argument within the function doesn't affect the original object outside the function's scope. This mimics "pass by value" behavior.
Python pass by value example def modify_number(x): x += 10 print("Inside function:", x) my_num = 5 modify_number(my_num) print("Outside function:", my_num)

Output

Inside function: 15 Outside function: 5
`my_num` remains unchanged because a copy of its value was passed. The function created a new integer object (15) and bound `x` to it.
Similarly, with strings:
Python pass by value example with string type def modify_string(s): s += " world" print("Inside function:", s) my_string = "hello" modify_string(my_string) print("Outside function:", my_string)

Output

Inside function: hello world Outside function: hello
Again, the original string is untouched; a new string object was created inside the function.

2. Mutable Objects (Pass by Reference Behavior)

Mutable objects (lists, dictionaries, sets) behave differently. When you pass a mutable object to a function, the function receives a reference to the original object. Any modifications made within the function directly affect the original object. This is closer to "pass by reference."
Python pass by reference using list type def modify_list(my_list): my_list.append(4) print("Inside function:", my_list) my_list = [1, 2, 3] modify_list(my_list) print("Outside function:", my_list)

Output

Inside function: [1, 2, 3, 4] Outside function: [1, 2, 3, 4]
The `append()` method modifies the original list directly. The function doesn't create a copy; it works on the same list object in memory.
Using dictionary type:
Python pass by reference using dictionary def modify_dict(my_dict): my_dict["new_key"] = "new_value" print("Inside function:", my_dict) my_dict = {"a": 1, "b": 2} modify_dict(my_dict) print("Outside function:", my_dict)

Output

Inside function: {'a': 1, 'b': 2, 'new_key': 'new_value'} Outside function: {'a': 1, 'b': 2, 'new_key': 'new_value'}
The dictionary is modified in place.

3. Important Note: Reassignment vs. Modification

Even with mutable objects, if you reassign the variable inside the function, it only affects the local copy of the reference. The original object remains unchanged.
Python passing argument example def reassign_list(my_list): my_list = [5, 6, 7] # Reassignment, not modification print("Inside function:", my_list) my_list = [1, 2, 3] reassign_list(my_list) print("Outside function:", my_list)

Output

Inside function: [5, 6, 7] Outside function: [1, 2, 3]
Inside `reassign_list`, `my_list` is reassigned to a completely new list. The original list outside the function is not affected.

Tutorials